package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MarketingAdsMapper;
import cc.rengu.igas.bomp.common.dao.impl.MarketingAdsMapperImpl;
import cc.rengu.igas.bomp.common.entity.MarketingAds;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MarketingAdsInfoBean;
import cc.rengu.igas.bomp.facade.request.MarketingAdsInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MarketingAdsInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销广告信息查询
 * Created by wangcheng on 2021/7/2 14:44
 */
public class MarketingAdsInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MarketingAdsInfoQueryRequest marketingAdsInfoQueryRequest = new MarketingAdsInfoQueryRequest();
        ConvertUtil.convertOutput(marketingAdsInfoQueryRequest);
        return marketingAdsInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MarketingAdsInfoQueryRequest marketingAdsInfoQueryRequest = (MarketingAdsInfoQueryRequest) request;
        MarketingAdsInfoQueryResponse marketingAdsInfoQueryResponse = new MarketingAdsInfoQueryResponse();
        marketingAdsInfoQueryResponse.setHeader(marketingAdsInfoQueryRequest.getHeader());
        String instId = marketingAdsInfoQueryRequest.getHeader().getInstId();
        String adType = marketingAdsInfoQueryRequest.getAdType();
        MarketingAdsMapper marketingAdsMapper = new MarketingAdsMapperImpl();
        List<MarketingAds> marketingAdsList = marketingAdsMapper.selectMarketingAdsListByInstIdAndAdType(instId, adType);
        if (null != marketingAdsList && !marketingAdsList.isEmpty()) {
            List<MarketingAdsInfoBean> marketingAdsInfoBeanList = new ArrayList<>();
            marketingAdsList.forEach(item -> convMarketingAdsInfo(item, marketingAdsInfoBeanList));
            marketingAdsInfoQueryResponse.setMarketingAdsInfoBeanList(marketingAdsInfoBeanList);
        }
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(marketingAdsInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MarketingAdsInfoQueryResponse marketingAdsInfoQueryResponse = (MarketingAdsInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(marketingAdsInfoQueryResponse);
    }

    /**
     * 营销广告信息从数据库字段转换成接口字段
     *
     * @param marketingAds             数据库营销广告信息
     * @param marketingAdsInfoBeanList 接口营销广告信息
     */
    void convMarketingAdsInfo(MarketingAds marketingAds, List<MarketingAdsInfoBean> marketingAdsInfoBeanList) {
        MarketingAdsInfoBean marketingAdsInfoBean = new MarketingAdsInfoBean();
        BeanUtil.beanCopy(marketingAds, marketingAdsInfoBean);
        marketingAdsInfoBeanList.add(marketingAdsInfoBean);
    }
}
